package com.df.excel_export.validator.vadator_impl.type_int;

import com.df.excel_export.common.ExcelTable;
import com.df.excel_export.common.ExcelTableManager;
import com.df.excel_export.validator.AbstractIFieldValidator;
import com.df.excel_export.validator.IFieldValidator;
import com.df.excel_export.validator.IValidatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * int类型的外键校验器
 */
@Component
//表达式：fk(其他表表名)
public class IntFkValidator extends AbstractIFieldValidator implements IValidatorFactory {
    public String fk_TableName;//关联的外键名字
    private static Logger log = LoggerFactory.getLogger(IntFkValidator.class.getName());

    @Override
    public boolean support(String filedType, String ruleStr) {
        if(!filedType.equals("int")){
            return false;
        }
        ruleStr  = ruleStr.trim();
        if(ruleStr.startsWith("fk(") && ruleStr.endsWith(")")){
            return true;
        }
        return false;
    }

    @Override
    public IFieldValidator create(String filedType, String ruleStr) {
        String fkName = ruleStr.trim().substring(3, ruleStr.length() - 1);
        IntFkValidator validator = new IntFkValidator();
        validator.fk_TableName = fkName;
        validator.getDepends().add(fk_TableName);
        return validator;
    }

    @Override
    public Integer getPriority() {
        return 0;
    }


    @Override
    public boolean validate(ExcelTable checkTable,List<Object> rowData, Integer dataDowIndex, Integer fieldIndex, String fieldNameString , String fieldTypeStr, Object filedValue) {
        //从tablename对应的表中找id列，检测fielValue是否在id列中
//        ExcelTable fkTable = ExcelTableManager.tableName2ExcelTable.get(fk_TableName);
//        if(fkTable == null){
//            log.error("检测关联外键时，外键所在的表不存在！外键表名："+fk_TableName);
//            return false;
//        }
//        Set<Object> ids = fkTable.getIds();
//        if(!ids.contains(filedValue)){//单元格的值不在外键表id集合中
//
//            return false;
//        }
        return true;
    }
}
